package jp.co.nes.awssupport.common.utility;

import java.util.List;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.redshift.AmazonRedshift;
import com.amazonaws.services.redshift.AmazonRedshiftClientBuilder;
import com.amazonaws.services.redshift.model.DescribeReservedNodesRequest;
import com.amazonaws.services.redshift.model.DescribeReservedNodesResult;
import com.amazonaws.services.redshift.model.ReservedNode;
import com.amazonaws.services.redshift.model.ReservedNodeNotFoundException;

import jp.co.nes.awssupport.common.base.CommonException;

/**
 * Redshiftヘルパーです。<br>
 * Redshiftに関する機能を提供します。
 */
public class AwsRedshiftHelper extends AwsHelperBase {

    /** クライアント */
    private AmazonRedshift client;

    /**
     * デフォルトコンストラクタです。
     */
    public AwsRedshiftHelper() {
    }

    /**
     * リージョンを引数にもつコンストラクタです。
     *
     * @param regions リージョン
     * @param accessKey アクセスキー
     * @param secretKey シークレットキー
     */
    public AwsRedshiftHelper(Regions regions, String accessKey, String secretKey) {
        super(regions, accessKey, secretKey);
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.common.utility.AwsHelperBase#init(com.amazonaws.regions.Regions, com.amazonaws.auth.AWSCredentials, com.amazonaws.ClientConfiguration)
     */
    @Override
    protected void init(Regions regions, AWSCredentials credentials, ClientConfiguration config) {
        boolean isLocalTest = Boolean.valueOf(PropertiesUtil.getProperty("LocalTest", "false"));
        if (!isLocalTest) {
            if (credentials == null) {
                if (config == null) {
                    client = AmazonRedshiftClientBuilder.standard()
                            .withRegion(regions)
                            .build();
                } else {
                    client = AmazonRedshiftClientBuilder.standard()
                            .withRegion(regions)
                            .withClientConfiguration(config)
                            .build();
                }
            } else {
                if (config == null) {
                    client = AmazonRedshiftClientBuilder.standard()
                            .withRegion(regions)
                            .withCredentials(new AWSStaticCredentialsProvider(credentials))
                            .withClientConfiguration(new ClientConfiguration())
                            .build();
                } else {
                    client = AmazonRedshiftClientBuilder.standard()
                            .withRegion(regions)
                            .withCredentials(new AWSStaticCredentialsProvider(credentials))
                            .withClientConfiguration(config)
                            .build();
                }
            }
        }
    }

    /**
     * ReservedNodeを返却します。<br>
     * reservedNodeIdに対応するReservedNodeがなかった場合はnullを返却します。
     *
     * @param reservedNodeId reservedNodeId
     * @return ReservedNode
     */
    public ReservedNode getReservedNode(String reservedNodeId) {
        DescribeReservedNodesRequest request = new DescribeReservedNodesRequest();
        request.setReservedNodeId(reservedNodeId);
        DescribeReservedNodesResult result = null;
        try {
            result = client.describeReservedNodes(request);
        } catch (ReservedNodeNotFoundException e) {
            // 存在しなかった場合、何もせず、処理を続行する。
        }

        ReservedNode reservedNode = null;
        if (result != null && result.getReservedNodes() != null) {
            List<ReservedNode> reservedNodeList = result.getReservedNodes();
            int size = reservedNodeList.size();
            if (size == 1) {
                reservedNode = reservedNodeList.get(0);
            } else if (size > 1) {
                throw new CommonException("size > 1(size=" + size + ")");
            }
        }
        return reservedNode;
    }
}
